(function() {
    var config = {
        ethereum: {
            chainId: "%1$s",
            rpcUrl: "%2$s",
            address: "%3$s",
        },
        solana: {
            cluster: "mainnet-beta",
            address: "%4$s",
        },
        isDebug: false
    };
    mixinwallet.ethereum = new mixinwallet.Provider(config);
    mixinwallet.solana = new mixinwallet.SolanaProvider(config);
    mixinwallet.postMessage = (json) => {
        window._mw_.postMessage(JSON.stringify(json));
    }
    window.ethereum = mixinwallet.ethereum;
    window.solana = mixinwallet.solana;

    const mixinLogoDataUrl = '';
    const info = {
        uuid: crypto.randomUUID(),
        name: 'Mixin Messenger',
        icon: mixinLogoDataUrl,
        rdns: 'one.mixin.messenger',
    };

    window.ethereum.setAddress(config.ethereum.address);
    if (config.solana.address) {
        window.solana.setAddress(config.solana.address);
    }

    function initializeEIP6963(provider, options = {}) {
        const providerDetail = {
            info,
            provider
        };
        Object.defineProperty(providerDetail, 'provider', {
            get() {
                options.onAccessProvider?.();
                return provider;
            },
        });
        const announceEvent = new CustomEvent('eip6963:announceProvider', {
            detail: Object.freeze(providerDetail),
        });
        window.dispatchEvent(announceEvent);
        window.addEventListener('eip6963:requestProvider', () => {
            window.dispatchEvent(announceEvent);
            options.onRequestProvider?.();
        });
    }
    initializeEIP6963(window.ethereum);
})();